package flyingpig.servlet;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;

import com.thoughtworks.xstream.XStream;

import flyingpig.data.dao.ImageInfoHome;
import flyingpig.data.dao.JobPageHome;
import flyingpig.data.dao.SearchJobHome;
import flyingpig.data.domain.JobPage;
import flyingpig.data.domain.SearchJob;
import flyingpig.hibernate.HibernateUtil;

public class FinishJobPage extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private   static   Log   logger   =   LogFactory.getLog(FinishJobPage.class);
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FinishJobPage() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text/html");

        PrintWriter out = response.getWriter();
        int pageID = -1;
        try {
            pageID = Integer.parseInt(request.getParameter("pageID"));
        } catch (Exception e) {
            out.println("status: pageID is wrong");
            out.flush();
            out.close();
            return;
        }
        if (pageID < 0) {
        	out.println("status: pageID is wrong");
            out.flush();
            out.close();    
            return;
        }
        
		Transaction tx = HibernateUtil.getCurrentSession().beginTransaction();
		try {
			// page是否存在
			JobPageHome jobPageHome = new JobPageHome();
			JobPage page = jobPageHome.findById(pageID);
			
			if (page == null) {
				logger.info("status: pageID is wrong");
	            out.println("status: pageID is wrong");
	            out.flush();
	            out.close();
	            return;
			}
			
			if (page.getPageState().equals("COMPLETE")) {
				logger.info("status: page is already finished");
	            out.println("status: page is already finished");
	            out.flush();
	            out.close();
	            return;
			}

			page.setPageState("COMPLETE");
			page.setLastUpdate(new Date());
			jobPageHome.persist(page);
			SearchJobHome searchJobDao = new SearchJobHome();
			SearchJob searchJob = searchJobDao.findById(page.getSearchJobId());
			searchJob.setFinishedNum(searchJob.getFinishedNum() + 1);
			logger.info(searchJob.getSearchJobId() + "'s searchJob finished Num: " + searchJob.getFinishedNum());
			searchJobDao.persist(searchJob);
            out.println("status: transaction success");
            out.flush();
            out.close();
			tx.commit();
			
		} catch (HibernateException e) {

			logger.info("status: transaction error , rollback");
			tx.rollback();
			e.printStackTrace();
            out.println("status: transaction error");
            out.flush();
            out.close();
            return;
		}
	}

}
